package org.thot.model.ge 
{
	/**
	 * ...
	 * @author Thot
	 */
	public class GameEvent 
	{
		
		protected var m_trigger:String;
		protected var m_name:String;
		
		public var pool:GameEventPool;
		public var scriptAPI:Object;
		
		public var persistance:int;
		protected var m_infinite:Boolean;

		
		
		public function GameEvent( p_name:String, p_trigger:String, p_persistance:int = -1 ) 
		{
			init( p_name, p_trigger, p_persistance );
		}
		
		private function init(p_name:String, p_trigger:String, p_persistance:int):void
		{
			persistance = p_persistance;
			m_infinite = ( p_persistance == -1 );
			m_name = p_name;
			m_trigger = p_trigger;
			scriptAPI = null;
			pool = null;
		}
		
		
		protected function callAPI ( p_funcName:String, p_parameters:Array ):*
		{
			var l_func:Function = scriptAPI[ p_funcName ] as Function;
			if ( l_func == null )
			{
				return null;
			}
			else
			{
				return l_func.apply( this, p_parameters );
			}
		}
		
		public function exec():void
		{
			process();
			
			if ( !m_infinite )
			{
				persistance--;
				
				if ( persistance <= 0 )
				{
					destroy();
				}
			}
		}
		
		public function process():void
		{
			//code
		}
		
		public function get trigger():String { return m_trigger; }
		public function get name():String { return m_name; }
		
		public function destroy():void
		{
			
			if ( pool != null )
			{
				pool.removeGameEvent( this );
			}
			
			pool = null;
			scriptAPI = null;
			m_name = null;
			m_trigger = null;
			delete this;
		}
		
	}

}